//
//  PersonInformationViewController.m
//  FlowerRainGround
//
//  Created by lanou on 16/4/20.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "PersonInformationViewController.h"
#import "UserInformationView.h"
@interface PersonInformationViewController ()< UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollerView;
@property (nonatomic, strong) UserInformationView *userView;//用户设置的视图

@end

@implementation PersonInformationViewController


- (void)setNavBar{
    self.navigationItem.title = @"个人资料";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 50, 25);
    [button setTitle:@"保存" forState:UIControlStateNormal];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:button];
    button.block = ^(){
        [self save];
    };
    self.navigationItem.rightBarButtonItem = right;
}

//点击保存的方法
- (void)save
{
    if (![[userManager getUsrID] isEqualToString:@" "]) {
        NSLog(@"有人");
        //保存用户头像
//        [userManager saveuserHeadImage:self.userView.headImageView.image];
        [userManager saveuserName:self.userView.nameTextField.text];
        
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBar];
    
    //创建滚动视图
    self.scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    self.scrollerView.contentSize = CGSizeMake(kWidth, 1.2*kHeight);
    self.scrollerView.showsVerticalScrollIndicator = YES;
    
    //自定义视图
    _userView = [[UserInformationView alloc] initWithFrame:self.view.bounds];
    [self.scrollerView addSubview:_userView];
    [self.view addSubview:self.scrollerView];
    
    //添加轻拍手势
    _userView.changeImageLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap: )];
    [_userView.changeImageLabel addGestureRecognizer:tapGesture];
    

    
}


//轻拍头像选择头像
- (void) tap:(UITapGestureRecognizer *)tapGesture{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"选择头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //如果有相机
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
            pickerVC.delegate = self;
            pickerVC.allowsEditing = YES;
            
            [pickerVC presentViewController:pickerVC animated:YES completion:nil];
        }
 
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
    }]];
    
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [alertVC dismissViewControllerAnimated:YES completion:nil];
    }]];
    
    [self presentViewController:alertVC animated:YES completion:nil];

}

//从相册中获取头像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    self.userView.headImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:nil];
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
